package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.ExpressCompany;
import com.dingreading.cloud.admin.entity.table.ExpressCompanyTableDef;
import com.dingreading.cloud.admin.mapper.ExpressCompanyMapper;
import com.dingreading.cloud.admin.service.ExpressCompanyService;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class ExpressCompanyServiceImpl extends BaseServiceImpl<ExpressCompanyMapper, ExpressCompany, ExpressCompanyTableDef> implements ExpressCompanyService {

    @Override
    protected ExpressCompanyTableDef getTable() {
        return ExpressCompanyTableDef.expressCompany;
    }

    @Override
    public Page<ExpressCompany> pageList(PageUtil pageUtil, String name, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(name))
            condition.and(table.name.like(name));
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.enabled.desc(), table.addTime.asc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public void updateEnabled(List<Long> idList, Integer enabled) {
        UpdateChain.of(ExpressCompany.class)
                .set(table.enabled, enabled)
                .where(table.id.in(idList))
                .update();
    }

    @Override
    public List<ExpressCompany> getByEnabled(String name) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(name)) {
            condition.and(table.name.like(name));
        }
        condition.and(table.enabled.eq(1));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.asc());

        return getMapper().selectListByQuery(wrapper);
    }
}
